<?php
include_once("../../commonFunctions.php");
?>
<!DOCTYPE HTML>
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
        <META HTTP-EQUIV="refresh" CONTENT="5;URL=/main.php"/>
        <link rel="stylesheet" type="text/css" href="/CSS/formStyle.css" />
    </head>
    <body >
        <h1 id="logo">Cellular Sales - South Georgia TTS</h1>
        <div id="header">
            <p id="message">Redirecting to Main in 5 seconds</p>
        </div>
        <br/><br/><br/><br/>
        <?php
            $store = new Store($_SESSION['storeAbbrev']);
            $dealer = new Dealer($_SESSION['dealerCode']);
            $tableName = 'empty';
            $type = 'empty';
            $description = 'empty';
            $pcLabel = 'empty';
            $select5 = 'empty';
            $notes = 'empty';
            if (isset($_POST['select1'])) {
                $tableName = $_POST['select1'];
            }
            if ($tableName == 'Maintenance' || $tableName == 'Merchandising') {
                if ($tableName == 'Maintenance') {
                    $tableName = 'maintenance';
                }
                if ($tableName == 'Merchandising') {
                    $tableName = 'merchandising';
                }
                if (isset($_POST['select2'])) {
                    $type = $_POST['select2'];
                }
                if (isset($_POST['select3'])) {
                    $description = $_POST['select3'];
                }
                if (isset($_POST['notes'])) {
                    $notes = $_POST['notes'];
                }

                $query = 'select * from ' . $tableName . ' where type=\'' . $type . '\' and description = \'' . $description . '\';';
                $result = mysql_query($query) or die('failed in insertTT');
                $row = mysql_fetch_array($result);
                $level = $row['priority'];
                $id = $row['id'];

                $result1 = mysql_query('select maxHours from priority where level = \'' . $level . '\';');
                $row = mysql_fetch_array($result1);
                $hours = $row['maxHours'];
                $todayDate = date("Y-m-d g:i a");
                $currentTime = time($todayDate);
                $timeAfterOneHour = $currentTime + 60 * 60 * $hours;
                $adjustedDateTime = date("Y-m-d H:i:s", $timeAfterOneHour);
                $assignedDeal = '';
                if ($tableName == 'maintenance') { // maintenance
                    $assignedDeal = 'sgnckelly';
                } else { // merchandising
                    if ($store->storeAbbrev != 'SM' && ($store->state == 'GA' || $store->state == 'SC')) {
                        $assignedDeal = 'sg0326';
                    } else {
                        $assignedDeal = 'sg0256';
                    }
                }
                $query = 'insert into tt values(null, \'' . $store->market . '\', \'' . $store->storeAbbrev . '\', \'' . $tableName
                        . '\', \'' . $id . '\', \'' . $dealer->dealerCode . '\', \'' . $assignedDeal . '\', \'' . date("Y-m-d H:i:s")
                        . '\', \'' . $adjustedDateTime . '\', null, \'Submitted\');';
                mysql_query($query) or die('failed while inserting tt record<br/>' . $query);
                $id = mysql_insert_id();

                // create new ttstatus1m
                $query = 'insert into ttstatus1m values(' . $id . ', null );';
                mysql_query($query) or die('failed while inserting ttstatus1m record');
                $id = mysql_insert_id();
                // create new ttstatuschange
                $query = 'insert into ttstatuschange values(' . $id . ', \'' . date("Y-m-d H:i:s") . '\', \'' . $dealer->dealerCode
                        . '\', \'' . $notes . '\', \'Submitted\'  );';
                mysql_query($query) or die('failed while inserting ttstatuschange');

                $assignedDealer = new Dealer($assignedDeal);
                echo '<center>Your trouble ticket has been successfully submited and has been assigned to
        ' . $assignedDealer->firstName . ' ' . $assignedDealer->lastName . '.<br/></center>';
                echo '<center>Your trouble ticket currently has an Submitted status.<br/>Whenever the status is changed to read, in progress,
            or resolved you will receive emails updating you.</center>';
                echo '<br/><br/><center>Redirect user to main.php</center>';

                //send email
            } else if ($tableName == 'Computer') {
                $tableName = 'computerissues';
                if (isset($_POST['select2'])) {
                    $pcLabel = $_POST['select2'];
                }
                if (isset($_POST['select3'])) {
                    $type = $_POST['select3'];
                }
                if (isset($_POST['select4'])) {
                    $description = $_POST['select4'];
                }
                if (isset($_POST['notes'])) {
                    $notes = $_POST['notes'];
                }
                $query = 'select * from computerissues where type=\'' . $type . '\' and description = \'' . $description . '\';';
                $result = mysql_query($query) or die('failed in insertTT');
                $row = mysql_fetch_array($result);
                $level = $row['priority'];
                $id = $row['id'];

                $result1 = mysql_query('select maxHours from priority where level = \'' . $level . '\';');
                $row = mysql_fetch_array($result1);
                $hours = $row['maxHours'];
                $todayDate = date("Y-m-d g:i a");
                $currentTime = time($todayDate);
                $timeAfterOneHour = $currentTime + 60 * 60 * $hours;
                $adjustedDateTime = date("Y-m-d H:i:s", $timeAfterOneHour);
                $assignedDealer = new Dealer('sgnctaylor');


                $query = 'insert into tt values(null, \'' . $store->market . '\', \'' . $store->storeAbbrev . '\', \'' . $tableName
                        . '\', \'' . $id . '\', \'' . $dealer->dealerCode . '\', \'' . $assignedDealer->dealerCode . '\', \'' . date("Y-m-d H:i:s")
                        . '\', \'' . $adjustedDateTime . '\', \'' . $pcLabel . '\', \'Submitted\');';
                mysql_query($query) or die('failed while inserting tt record<br/>' . $query);
                $id = mysql_insert_id();

                // create new ttstatus1m
                $query = 'insert into ttstatus1m values(' . $id . ', null );';
                mysql_query($query) or die('failed while inserting ttstatus1m record');
                $id = mysql_insert_id();
                // create new ttstatuschange
                $query = 'insert into ttstatuschange values(' . $id . ', \'' . date("Y-m-d H:i:s") . '\', \'' . $dealer->dealerCode
                        . '\', \'' . $notes . '\', \'Submitted\'  );';
                mysql_query($query) or die('failed while inserting ttstatuschange');

                echo '<center>Your trouble ticket has been successfully submited and has been assigned to
        ' . $assignedDealer->firstName . ' ' . $assignedDealer->lastName . '.<br/></center>';
                echo '<center>Your trouble ticket currently has an Submitted status.<br/>Whenever the status is changed to read, in progress,
            or resolved you will receive emails updating you.</center>';
                echo '<br/><br/><center>Redirect user to main.php</center>';

                //send email
            }
        ?>
    </body>
</html>
